package com.example.test_01.adapter;

import static android.content.ContentValues.TAG;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.test_01.R;
import com.example.test_01.model.Orders;
import com.example.test_01.service.OrderService;
import com.example.test_01.service.OrderServiceImpl;
import com.example.test_01.service.UserService;
import com.example.test_01.service.UserServiceImpl;

import java.util.List;

public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.ViewHolder>{

    private List<Orders> list;
    private final OrderService service = new OrderServiceImpl();
    private final UserService userService = new UserServiceImpl();

    private void list(){
        list = service.list();
    }
    public OrderAdapter(){ list(); }
    public void reload(){
        list();
        notifyDataSetChanged();
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        TextView text_user_name;
        TextView text_price_sum;
        TextView text_create_time;
        RecyclerView recyclerView;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            text_user_name = itemView.findViewById(R.id.text_user_name);
            text_price_sum = itemView.findViewById(R.id.text_price_sum);
            text_create_time = itemView.findViewById(R.id.text_create_time);
        }
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.order_show,parent,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Orders orders = list.get(position);
        Log.d(TAG, "onBindViewHolder: 订单信息" + orders.toString());
        Log.d(TAG, "onBindViewHolder: orders.getUserId()" + orders.getUserId());
        Log.d(TAG, "onBindViewHolder: userService.getUserById(orders.getUserId())" + userService.getUserById(orders.getUserId()));
        Log.d(TAG, "onBindViewHolder: userService.getUserById(orders.getUserId()).getNickname()" + userService.getUserById(orders.getUserId()).getNickname());
        holder.text_user_name.setText(userService.getUserById(orders.getUserId()).getNickname());
        holder.text_price_sum.setText(orders.getSum() + "￥");
        holder.text_create_time.setText(orders.getCreateTime().toString());
//        FoodOrderAdapter adapter = new FoodOrderAdapter(orders.getFoodList());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
}
